package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.QueryTeamVo;
import com.kkb.pojo.ResultVo;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.service.TeamService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("team")
public class TeamController {

    @Resource
    private TeamService teamService;

    @RequestMapping(value = "list.do",method = RequestMethod.GET)
    public ResultVo<Team> teamList(Integer pageNum, Integer pageSize, QueryTeamVo vo){
        if (pageNum == null || pageNum<= 0) {
            pageNum = 0;
        }
        if (pageSize == null || pageSize<= 0) {
            pageSize = 5;
        }
        PageInfo<Team> teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo<>(teamPageInfo);
    }

    @RequestMapping(value = "add.do",method = RequestMethod.POST)
    public ResultVo<Team> teamAdd(Team team){
        System.out.println(team.toString());
        Integer integer = teamService.addTeam(team);
        if (integer > 0) {
            return new ResultVo<Team>();
        }
        return new ResultVo<Team>("500","服务器内部错误，请稍后再试");
    }

    @RequestMapping(value = "{id}.do",method = RequestMethod.GET)
    public ResultVo<Team> teamQueryById(@PathVariable("id") Integer teamId){
        Team team = teamService.queryById(teamId);
        System.out.println(team.toString());
        if (team != null && !team.getTeamName().equals("")) {
            return new ResultVo<Team>(team);
        }
        return new ResultVo<Team>("500","服务器内部错误，请稍后再试");
    }

    @RequestMapping(value = "{id}.do",method = RequestMethod.PUT)
    public ResultVo<Team> teamUpdate(@PathVariable("id") Integer teamId,Team team){
        team.setTeamId(teamId);
        System.out.println(team.toString());
        Integer integer = teamService.updateTeam(team);
        if (integer == 1) {
            return new ResultVo<Team>(team);
        }
        return new ResultVo<Team>("500","服务器内部错误，请稍后再试");
    }

    @RequestMapping(value = "{id}.do",method = RequestMethod.DELETE)
    public ResultVo<Team> teamDelete(@PathVariable("id") Integer teamId){
        System.out.println(teamId);
        Integer integer = teamService.deleteTeam(teamId);
        if (integer == 1) {
            return new ResultVo<>();
        }
        return new ResultVo<>("500","服务器内部错误，请稍后再试");
    }

    @RequestMapping(value = "upload/{id}.do",method = RequestMethod.POST)
    public ResultVo<Team> fileUpload(@RequestParam("logo")MultipartFile myfile, HttpServletRequest request,@PathVariable("id") Integer teamId){
        //获取文件要存储的位置的绝对路径
        String path = request.getServletContext().getRealPath("/img/uploadFile/");
        //拿到文件的原始名称
        String originalFilename = myfile.getOriginalFilename();
        //uuid拼接生成随即名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i);
        String logoName = randomName + substring;
        int num=0;
        try {
            myfile.transferTo(new File(path+"/"+logoName));
            System.out.println("上传成功！"+path+"/"+logoName);
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(logoName);
            Integer integer = teamService.updateTeam(team);
            if (integer == 1) {
                return new ResultVo<>();
            }
            return new ResultVo<>("500","服务器内部错误，请稍后再试");

        } catch (IOException e) {
            return new ResultVo<>("500","上传出错，错误代码："+e.getMessage());
        }
    }
}
